import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { Meeting, User, MeetingParam, UserParam } from './model';

/**
 * 分页查询角色
 */
export async function pageMeeting(params: MeetingParam) {
  const res = await request.get<ApiResult<PageResult<Meeting>>>(
    '/sign/meeting/page',
    { params }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询用户列表
 */
export async function listUsers(params?: UserParam) {
  const res = await request.get<ApiResult<User[]>>('/sign/users', {
    params
  });
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加
 */
export async function addMeeting(data: Meeting) {
  const res = await request.post<ApiResult<unknown>>('/sign/meeting', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改
 */
export async function updateMeeting(data: Meeting) {
  const res = await request.put<ApiResult<unknown>>('/sign/meeting', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除
 */
export async function removeMeeting(id?: number) {
  const res = await request.delete<ApiResult<unknown>>('/sign/meeting/' + id);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除角色
 */
export async function removeMeetingBatch(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>('/sign/meeting/batch', {
    data
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

export async function updateSignUser(data: User) {
  const res = await request.put<ApiResult<unknown>>('/sign/userSeat', data);
  if (res.data.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.message));
}
